package com.hhj.sharding.sphere.demo.enums;


import lombok.Getter;

import java.util.Objects;

/**
 * 通用状态枚举
 *
 * @Description
 * @Author HHJ
 * @Date 2022-02-28 18:28
 */
@Getter
public enum CommonStatusEnums implements BusinessEnum {
    /**
     * 通用状态枚举
     */
    DEFAULTS(0, "默认/新建"),
    UP(1, "上架/正常"),
    DOWN(2, "下架/禁用"),
    ;

    private Integer code;

    private String message;

    CommonStatusEnums(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static CommonStatusEnums getByCode(Integer code) {
        for (CommonStatusEnums value : CommonStatusEnums.values()) {
            if (value.getCode() == code) {
                return value;
            }
        }
        return null;
    }

    public static boolean isValid(Integer code) {
        if (Objects.isNull(code)) {
            return false;
        }
        return getByCode(code) != null;
    }

    /**
     * 是否是启用状态
     *
     * @param code
     * @return
     */
    public static boolean isUp(Integer code) {
        if (Objects.isNull(code)) {
            return false;
        }
        return CommonStatusEnums.UP.getCode() == code.intValue();
    }

    /**
     * 是否是非启用状态
     *
     * @param code
     * @return
     */
    public static boolean isNotUp(Integer code) {
        return !CommonStatusEnums.isUp(code);
    }

    /**
     * 是否是下架状态
     *
     * @param code
     * @return
     */
    public static boolean isDown(Integer code) {
        if (Objects.isNull(code)) {
            return false;
        }
        return CommonStatusEnums.DOWN.getCode() == code.intValue();
    }
}
